package lang;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author 李栋
 * @createtime 2021-06-02 08:17:49
 */
public class ExceptionTest {

    private static final Logger logger = LoggerFactory.getLogger(ExceptionTest.class);

    @Test
    public void typeJavaException() {
        Exception illegalArgumentException = new IllegalArgumentException("参数不正确！");
        logger.error("Result:{}", illegalArgumentException);

        Exception illegalAccessException = new IllegalAccessException("非法访问异常！");
        logger.error("Result:{}", illegalAccessException);

        Exception illegalStateException = new IllegalStateException("非法状态异常！");
        logger.error("Result:{}", illegalStateException);

        Exception illegalMonitorStateException = new IllegalMonitorStateException("监控状态异常！");
        logger.error("Result:{}", illegalMonitorStateException);


        Exception illegalThreadStateException = new IllegalThreadStateException("线程状态异常！");
        logger.error("Result:{}", illegalThreadStateException);
    }

    /**
     * 递归调用产生栈溢出 {@link StackOverflowError}
     * <p>
     * 入栈的方法深度越来越深
     */
    @Test
    public void stackOverFlowError() {
        while (true) {
            oneMethod();
        }
    }

    void oneMethod() {
        oneMethod();
    }

    /**
     * 对象的大小超过堆的大小：堆内存溢出
     */
    @Test
    public void outOfMemoryError() {
        StringBuilder strOne = new StringBuilder("test");
        for (int i = 0; i < 10000000; i++) {
            strOne.append(strOne);
        }
        logger.info(strOne.toString());
    }


}
